﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using YanShu.Entity;
using YanShu.Handler;
using YanShu.Utils;

namespace YanShu
{
    /// <summary>
    /// Page1.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Page
    {
        public static Login current;
        public Login()
        {
            InitializeComponent();
            current = this;
        }

        private void Login_Click(object sender, RoutedEventArgs e)
        {
            String account = this.account.Text;
            String password = this.password.Password;
            if (account.Trim() == "")
            {
                App.ShowBalloonTip("注意", "账号不能为空!");
                return;
            }
            if (password.Trim() == "")
            {
                App.ShowBalloonTip("注意", "密码不能为空");
                return;
            }
            this.loading.Visibility = Visibility.Visible;
            ShowGif();
            ClientChannelHandler.Instance.LoginToServer(account, password);

        }


        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.loading.Visibility = Visibility.Hidden;
        }

        private void Retrieve_Click(object sender, RoutedEventArgs e)
        {
            this.ResetPwdGrid.Visibility = Visibility.Visible;
        }

        private void SignUp_Click(object sender, RoutedEventArgs e)
        {
            this.signUpGrid.Visibility = Visibility.Visible;
        }
        private void ResetPassword_Server(object sender, RoutedEventArgs e)
        {
            String accOrMail = this.resetAccOrMail.Text;
            String password = this.resetPassword.Text;
            String verifyCode = this.resetVerifyCode.Text;
            this.resetAccOrMail.BorderBrush = Brushes.White;
            this.resetPassword.BorderBrush = Brushes.White;
            this.resetVerifyCode.BorderBrush = Brushes.White;
            if (accOrMail.Trim() == "")
            {
                this.resetAccOrMail.BorderBrush = Brushes.Red;
                return;
            }
            if (accOrMail.Length < 4)
            {
                this.resetAccOrMail.BorderBrush = Brushes.Red;
                return;
            }
            if (password.Trim() == "")
            {
                this.resetPassword.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.CheckPassword(password))
            {
                this.resetPassword.BorderBrush = Brushes.Red;
                return;
            }
            if (verifyCode.Trim() == "") {
                this.resetVerifyCode.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.IsVerifyCode(verifyCode))
            {
                this.resetVerifyCode.BorderBrush = Brushes.Red;
                return;
            }
            AccountDTO accountDto = new AccountDTO();
            accountDto.SetAccount(accOrMail);
            password = CommonUtils.CreateMD5Hash(password);
            accountDto.SetPassword(password);
            accountDto.SetMac(verifyCode);
            this.loading.Visibility = Visibility.Visible;
            ShowGif();
            ClientChannelHandler.Instance.ResetPassowrdToServer(accountDto);
        }
        private void SignUp_Server(object sender, RoutedEventArgs e)
        {
            String email = this.signUpEmail.Text;
            String account = this.signUpAccount.Text;
            String password = this.signUpPassword.Text;
            String verifyCode = this.signUpVerifyCode.Text;
            this.signUpEmail.BorderBrush = Brushes.White;
            this.signUpAccount.BorderBrush = Brushes.White;
            this.signUpPassword.BorderBrush = Brushes.White;
            this.signUpVerifyCode.BorderBrush = Brushes.White;

            if (email.Trim() == "")
            {
                // App.ShowBalloonTip("注意", "邮箱不能为空!");
                this.signUpEmail.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.IsEmail(email))
            {
                // App.ShowBalloonTip("注意", "邮箱格式不正确!");
                this.signUpEmail.BorderBrush = Brushes.Red;
                return;
            }
            if (account.Trim() == "")
            {
                // App.ShowBalloonTip("注意", "账号不能为空!");
                this.signUpAccount.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.CheckAccount(account))
            {
                //  App.ShowBalloonTip("注意","账号格式不正确!");
                this.signUpAccount.BorderBrush = Brushes.Red;
                return;
            }
            if (password.Trim() == "")
            {
                // App.ShowBalloonTip("注意", "密码不能为空");
                this.signUpPassword.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.CheckPassword(password))
            {
                // App.ShowBalloonTip("注意", "密码格式不正确!");
                this.signUpPassword.BorderBrush = Brushes.Red;
                return;
            }
            if (verifyCode.Trim() == "")
            {
                this.signUpVerifyCode.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.IsVerifyCode(verifyCode))
            {
                this.signUpVerifyCode.BorderBrush = Brushes.Red;
                return;
            }
            this.loading.Visibility = Visibility.Visible;
            ShowGif();
            AccountDTO accountDto = new AccountDTO();
            accountDto.SetEmail(email);
            accountDto.SetAccount(account);
            accountDto.SetMac(verifyCode);
            password = CommonUtils.CreateMD5Hash(password);
            accountDto.SetPassword(password);
            ClientChannelHandler.Instance.SignUpToServer(accountDto);
        }
        private void Return_Click(object sender, RoutedEventArgs e)
        {
            this.signUpGrid.Visibility = Visibility.Hidden;
            this.ResetPwdGrid.Visibility = Visibility.Hidden;
        }
        private void Reset_Verify_Server(object sender, RoutedEventArgs e)
        {
            String accOrMail = this.resetAccOrMail.Text;
            String password = this.resetPassword.Text;
            this.resetAccOrMail.BorderBrush = Brushes.White;
            this.resetPassword.BorderBrush = Brushes.White;
            if (accOrMail.Trim() == "")
            {
                this.resetAccOrMail.BorderBrush = Brushes.Red;
                return;
            }
            if (accOrMail.Length < 4)
            {
                this.resetAccOrMail.BorderBrush = Brushes.Red;
                return;
            }
            if (password.Trim() == "")
            {
                this.resetPassword.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.CheckPassword(password))
            {
                this.resetPassword.BorderBrush = Brushes.Red;
                return;
            }
            AccountDTO accountDto = new AccountDTO();
            accountDto.SetAccount(accOrMail);
            accountDto.SetMac("resetPassword");
            ClientChannelHandler.Instance.VerifyCodeToServer(accountDto);
            resetVerifyBtn.IsEnabled = false;
            Countdown(Constants.RESET_PASSWORD);

        }

        private void Verify_Server(object sender, RoutedEventArgs e)
        {
            String email = this.signUpEmail.Text;
            String account = this.signUpAccount.Text;
            String password = this.signUpPassword.Text;
            this.signUpEmail.BorderBrush = Brushes.White;
            this.signUpAccount.BorderBrush = Brushes.White;
            this.signUpPassword.BorderBrush = Brushes.White;

            if (email.Trim() == "")
            {
                // App.ShowBalloonTip("注意", "邮箱不能为空!");
                this.signUpEmail.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.IsEmail(email))
            {
                // App.ShowBalloonTip("注意", "邮箱格式不正确!");
                this.signUpEmail.BorderBrush = Brushes.Red;
                return;
            }
            if (account.Trim() == "")
            {
                // App.ShowBalloonTip("注意", "账号不能为空!");
                this.signUpAccount.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.CheckAccount(account))
            {
                //  App.ShowBalloonTip("注意","账号格式不正确!");
                this.signUpAccount.BorderBrush = Brushes.Red;
                return;
            }
            if (password.Trim() == "")
            {
                // App.ShowBalloonTip("注意", "密码不能为空");
                this.signUpPassword.BorderBrush = Brushes.Red;
                return;
            }
            if (!CommonUtils.CheckPassword(password))
            {
                // App.ShowBalloonTip("注意", "密码格式不正确!");
                this.signUpPassword.BorderBrush = Brushes.Red;
                return;
            }
            AccountDTO accountDto = new AccountDTO();
            accountDto.SetEmail(email);
            accountDto.SetAccount(account);
            password = CommonUtils.CreateMD5Hash(password);
            accountDto.SetPassword(password);
            accountDto.SetMac("signUp");
            ClientChannelHandler.Instance.VerifyCodeToServer(accountDto);
            VerifyBtn.IsEnabled = false;
            Countdown(Constants.SIGNUP);

        }

        private int countSecond = 30; //记录秒数

        DispatcherTimer timer;


        private void Countdown(int type)
        {
            countSecond = 30;
            timer = new DispatcherTimer
            {
                Interval = new TimeSpan(0, 0, 0, 1)
            };
            timer.Tick += (e, a) => Timer_Tick(type, timer);
            timer.Start();
        }

        private void Timer_Tick(int type, DispatcherTimer timer)
        {
            if (countSecond == 0)
            {
                if (type == Constants.SIGNUP)
                {
                    VerifyBtn.Content = "重发";
                    VerifyBtn.IsEnabled = true;
                }
                if (type == Constants.RESET_PASSWORD)
                {
                    resetVerifyBtn.Content = "重发";
                    resetVerifyBtn.IsEnabled = true;
                }
                timer.Stop();
            }
            else
            {
                if (type == Constants.SIGNUP)
                {
                    VerifyBtn.Content = countSecond.ToString() + "s";
                }
                if (type == Constants.RESET_PASSWORD)
                {
                    resetVerifyBtn.Content = countSecond.ToString() + "s";
                }
                countSecond--;
            }
        }

        public static void ResetVerifyBtn(string type)
        {
            if (null != current.timer)
            {
                current.timer.Stop();
                if (type.Equals("signUp"))
                {
                    current.VerifyBtn.Content = "获取";
                    current.VerifyBtn.IsEnabled = true;
                }
                if (type.Equals("resetPassword")) {
                    current.resetVerifyBtn.Content = "获取";
                    current.resetVerifyBtn.IsEnabled = true;
                }
            }
        }

        public static void HideSignUpGrid()
        {
            current.signUpGrid.Visibility = Visibility.Hidden;
        }

        public static void HideResetPwdGrid()
        {
            current.ResetPwdGrid.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// 提示登录失败原因
        /// </summary>
        /// <param name="info"></param>
        public static void ShowInfo(String info)
        {
            current.info.Text = info;
        }

        public static void ShowGif()
        {
            current.info.Text = "";
            current.gif.Visibility = Visibility.Visible;
        }

        public static void HideGif()
        {
            current.gif.Visibility = Visibility.Hidden;
        }

        private void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow.PageDragMove();
        }

        public static void ShowLoading()
        {
            current.loading.Visibility = Visibility.Visible;
        }

        public static void HideLoading()
        {
            current.loading.Visibility = Visibility.Hidden;
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            App.AppShutdown();
        }
    }
}
